<?php

namespace app\model;

use think\Model;
use think\facade\Cache;
use think\facade\Log;

class MemberModel extends Model
{
    protected $table = 'members';

    // 关联成员的用户
    public function user()
    {
        return $this->belongsTo(UserModel::class, 'user_id');
    }

    // 关联成员的项目
    public function project()
    {
        return $this->belongsTo(ProjectModel::class, 'project_id');
    }

    // 关联成员的技能
    public function skills()
    {
        return $this->belongsToMany(Skill::class, 'member_skills', 'member_id', 'skill_id');
    }

    // 数据验证
    public static function validateData($data)
    {
        $validate = new \think\Validate([
            'user_id' => 'require|number',
            'project_id' => 'require|number',
            'status' => 'require|in:1,2', // 1: active, 2: inactive
        ]);

        return $validate->check($data);
    }

    // 获取成员的缓存
    public static function getMemberByIdWithCache($id)
    {
        $cacheKey = 'member_' . $id;
        $member = Cache::get($cacheKey);
        if (!$member) {
            $member = self::find($id);
            Cache::set($cacheKey, $member, 3600);  // 缓存1小时
        }
        return $member;
    }
}
